# 示例：抽象基类
from abc import ABC, abstractmethod
from enum import Enum
import json
from typing import Any, Optional

from openai import APIConnectionError
from pydantic import BaseModel

class ModelType(Enum):
    Azureopenai = "azureopenai"
    Qwen = "qwen"
    Deepseek = "deepseek"

class ModelInfo(BaseModel):
    model_type: ModelType
    base_url: str
    key: str
    model: str
    version: Optional[str] = None
    streaming: bool = False
    max_tokens: int = 2048
    temperature:float=0.7

# class BaseAIClient:
#     async def chat_completion(self, messages: List[dict], **kwargs) -> dict:
#         raise NotImplementedError

class BaseAIClient(ABC):
    def __init__(self, model_info: ModelInfo):
        """
        初始化客户端，设置模型信息
        :param model_info: 包含模型配置信息的对象
        """
        self.model_info = model_info

    @abstractmethod
    async def query(self, messages: list, **kwargs: Any) -> Any:
        """
        异步执行聊天补全任务
        :param messages: 对话消息列表
        :param kwargs: 其他可选参数
        :return: 返回聊天结果
        """
        pass

    # ----------------------
    # 公共工具方法（可选）
    # ----------------------
    # def _handle_api_error(self, error: Exception):
    #     """统一处理 API 错误"""
    #     self.logger.error(f"API Error: {str(error)}", exc_info=True)
    #     if isinstance(error, APIConnectionError):
    #         raise ServiceUnavailableError("无法连接到 AI 服务") from error
    #     raise AIClientError("AI 服务请求失败") from error

async def event_stream(response):
    for chunk in response:
        res_data={
            "content": chunk.choices[0].delta.content,
            "id": chunk.id,
            "created": chunk.created,
            "finish_reason": chunk.choices[0].finish_reason
        }
        yield f"data: {json.dumps(res_data)}\n\n"

# class SSEResponse(BaseModel):
#     id: Optional[str] = None
#     created: Optional[int] = None
#     content: Optional[str] = None
#     finish_reason: Optional[str] = None


# 功能
# 1. qwen stream √
# 2. qwen non stream
# 3. azureopenai stream
# 4. azureopenai non stream

